package com.ja.openchat.views;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.JsonObject;
import com.ja.openchat.R;
import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.util.XPopupUtils;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.manager.TgResponse;
import com.zy.openchat.core.manager.TgResultBlock;
import com.zy.openchat.core.model.message.ChatInfo;
import com.zy.openchat.core.model.user.UserInfo;

import org.apache.commons.lang3.StringUtils;

import java.util.Map;

import androidx.annotation.NonNull;

public class ChatUserModifyDialog extends BaseBottomDialog{

    private final UserInfo user;
    private final OnItemClickListner listner;
    private UserAvatarView avatarView;
    private ChatInfo chatInfo;


    public ChatUserModifyDialog(@NonNull Context context, UserInfo user,ChatInfo mChatInfo, OnItemClickListner listner) {
        super(context);
        this.user = user;
        this.listner = listner;
        chatInfo = mChatInfo;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_chat_user_modify_view;
    }


    @Override
    protected void onCreate() {
        super.onCreate();
        avatarView = findViewById(R.id.header_image_view);
        EditText headerName = findViewById(R.id.et_name);

        if(user != null){
            avatarView.showUser(user);
            headerName.setText(user.displayName());
        }
        findViewById(R.id.tv_save).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //保存
                if(TextUtils.isEmpty(headerName.getText().toString().trim())){
                    ToastUtils.showShort("请输入备注");
                    return;
                }
                saveUserNickname(headerName.getText().toString().trim());

            }
        });

        findViewById(R.id.iv_back).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

    }

    private void saveUserNickname(String name) {
        long toBeModifyUserId;
        if(user==null){
            toBeModifyUserId = chatInfo.userId();
        }else{
            toBeModifyUserId = user.id;
        }
        if (StringUtils.isEmpty(name)) {
            name = "";
        }
        showProgress();
        String finalName = name;
        TelegramManager.getTelegramManager().setContactNickName(getContext(), toBeModifyUserId, name, new TgResultBlock<TgResponse>() {
            @Override
            public void onResult(Map<String, Object> request, JsonObject response, TgResponse obj) {
                hideProgress();
                if (obj.ok) {//设置成功
                    listner.clickSave(finalName);
                    dismiss();
                } else {
                    showErrorTip(getContext().getString(R.string.request_error) + obj.errString);
                }
            }

            @Override
            public void onTimeout(Map<String, Object> request) {
                hideProgress();
                showErrorTip(getContext().getString(R.string.request_error));
            }
        });
    }

    @Override
    protected void onShow() {
        super.onShow();
    }

    @Override
    protected void onDismiss() {
        super.onDismiss();
    }

    @Override
    protected int getMaxHeight() {
        return (int) (XPopupUtils.getAppHeight(getContext()) * 0.98f);
    }


    public interface OnItemClickListner {
        void clickSave(String name);
    }

}
